home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: lafn.org!an234
- From: an234@lafn.org (Andres Lessing)
- Subject: Re: Arrays of strings
- X-Nntp-Posting-Host: lafn.org
- Message-ID: <1996Feb17.211357.13063@lafn.org>
- Sender: news@lafn.org
- Reply-To: an234@lafn.org (Andres Lessing)
- Organization: The Los Angeles Free-Net
- References: <Pine.SOL.3.91.960207141605.27357A-100000@nova>
- Date: Sat, 17 Feb 1996 21:13:57 GMT
-
-
- In a previous article, ldigiova@nova.umuc.edu (Larry DiGiovanni) says:
-
- >
- >I'm trying to read lines of a text file and load those lines into an
- >array. I have a couple of questions...
- >
- >1. How do I declare the array to contain the text. When it is declared,
- >I will not know how many lines are in the text file. char **txtarray?
- >
- >2. How do I allocate the space for the data once I know how many lines
- >are in the text file? txtarray = malloc((size_t)(nelements))?
- >
- >3. What is the best way to read from the text file into the array
- >elements? fscanf? fgets? I have had no success reading directly into the
- >elements with these two functions. I don't understand why, but it may be
- >related to my poor understanding of (1) and (2) above as well.
- >
- >Any help would be greatly appreciated. TIA
- >
- >Larry DiGiovanni
- >
- >
-
- My best guess would be to crate a structure as follows:
-
- #define MAXCHAR 128
-
- struct Text{
- char line[MAXCHAR];
- struct Text *next;
- }
-
- Then read a line and then create another structure using malloc:
-
- (struct Text *)malloc(sizeof(struct Text))
-
- then you don't have to worry about how many lines there are... just
- traverse the linked list. You however could create functions to count
- characters per line or number of lines.
-
-
-
- --
- Hofstadter's Law: | o__
- It always takes longer than you expect, even | _.</)_
- when you take Hofstadter's Law into account. | (_) \(_)
- " Godel Escher Bach" | Andres, an234@lafn.org
-